<template>
  <view class="page_content">
    <!-- <view class="stat_line">共{{total}}位会员</view> -->
    <!-- <scroll-view scrollY="true"
      class="scroll_box"
      v-if="listData.length"
      @scrolltolower="lower">
      <view class="user_item" v-for="(item,index) in listData" :key="index" @tap="gotoDetail(item.id,item.uid)">
        <image class="item_img" :src="item.headimgurl"></image>
        <view class="item_right">
          <view>{{item.real_name}}</view>
          <view>{{item.mobile}} </view>
          <view>
            账户余额：<text class="color_orange">{{item.balance}}</text>，
            账户积分：<text class="color_orange">{{item.point}}</text>
          </view>
          <view class="item_card">{{item.card_level}}</view>
        </view>
      </view>
      <AtLoadMore style="height:30px;margin-top:-10px" noMoreText="没有更多了" :status="loadingStatus"/>
    </scroll-view> -->
    <AtCheckbox
      :options="checkboxOption"
      :selectedList="checkedList"
      :onChange="handleChange"
    />
    <view class="bottom_block">没有更多了</view>
    <view class="bottom_button" @tap="send()">发送</view>
    <AtMessage />
  </view>
</template>

<script>
import Taro from '@tarojs/taro'
import './sendCoupon.module.scss'
import UserAPI from '../../../api/user'
export default {
  name: 'userList',
  data(){
    return{
      checkboxOption: [],
      checkedList: [],
      page: 1,
      total: 0
    }
  },
  mounted(){
    this.init();
  },
  methods:{
    init(){
      this.getListData()
    },
    async getListData(){
      var data = {
        page: this.page,
        is_exp: 0,
        app_id: Taro.getStorageSync('app_id')
      }
      const res = await UserAPI.getCouponList(data);
      if(res.code == 200){
        res.data.result.forEach(item => {
          var itemData = {
            value: item.id,
            label: item.title
          }
          var useableT = "有效期："
          if(item.is_term == 1){
            useableT += "领取后" + item.term_end_day + "天"
          }else{
            useableT += item.term_start_time + "~" + item.term_end_time
          }
          itemData.desc = useableT;
          this.checkboxOption.push(itemData);
        })
        if(this.checkboxOption.length != res.data.total_num){
          this.page ++;
          this.getListData();
        }
      }
    },
    handleChange (value) {
      this.checkedList = value
    },
    async send(){
      var data = {
        app_id: Taro.getStorageSync('app_id'),
        id: this.checkedList.join(','),
        uid: Taro.getStorageSync('detail_uid')
      }
      console.log(data);
      const res = await UserAPI.sendCoupon(data);
      if(res.code == 200){
        Taro.atMessage({
          message: '发送成功',
          type: "success"
        })
        Taro.navigateBack()
      }else{
        Taro.atMessage({
          message: res.msg,
          type: "error"
        })
      }
    }
  },
}
</script>